package com.feeyo.prophet.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.feeyo.prophet.pojo.action.fleet.FleetAttributesEventVo;
import com.feeyo.prophet.pojo.action.fleet.FleetAttributesResp;
import com.feeyo.prophet.pojo.action.flight.AirportIataResp;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * 机队信息辅助类
 */
public class FleetAttributesActionUtils {

    /**
     * 数据处理成需要的List
     */
    public static List<String> getAircraftList(List<String> aircraftStatusList) {
        List<String> aircraftList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(aircraftStatusList)) {
            for (int i = 0; i < aircraftStatusList.size(); i++) {
                String tmpAircraft = aircraftStatusList.get(i);
                aircraftList.add(tmpAircraft);
            }
        }
        return aircraftList;
    }

    /**
     * 机队事件明细信息
     */
    public static List<FleetAttributesEventVo> getOperatorEventDetail(String respTxt){
        JSONObject rootObject = JSON.parseObject(respTxt);
        List<FleetAttributesEventVo> fleetAttributesDetail = Lists.newArrayList();
        int code = rootObject.getIntValue("code");
        if (code == 200) {
            JSONArray rowsArray = rootObject.getJSONObject("data").getJSONArray("rows");
            if (rowsArray == null) {
                return fleetAttributesDetail;
            }
            fleetAttributesDetail = FlightScheduleActionUtils.getFleetAttributesEventDetail(rowsArray);
        }
        return fleetAttributesDetail;
    }

    /**
     * 机队信息详情
     */
    public static List<FleetAttributesResp> getOperatorDetail(String respTxt) {
        JSONObject rootObject = JSON.parseObject(respTxt);
        List<FleetAttributesResp> fleetAttributesDetail = Lists.newArrayList();
        int code = rootObject.getIntValue("code");
        if (code == 200) {
            JSONArray rowsArray = rootObject.getJSONObject("data").getJSONArray("rows");
            if (rowsArray == null) {
                return fleetAttributesDetail;
            }
            for (Object row : rowsArray) {
                FleetAttributesResp fleetAttributesResp = JSON.parseObject(String.valueOf(row), FleetAttributesResp.class);
                fleetAttributesDetail.add(fleetAttributesResp);
            }
        }
        return fleetAttributesDetail;
    }

    /**
     * 城市信息详情
     */
    public static List<String> getAirportIata(String host, int port, String cityIata) {
        Set<String> airportIataList = Sets.newHashSet();
        try {
            String result = DbSearchActionUtils.queryAirportIata(host, port, cityIata);
            JSONObject rootObject = JSON.parseObject(result);
            int code = rootObject.getIntValue("code");
            if (code == 200) {
                JSONArray rowsArray = rootObject.getJSONObject("data").getJSONArray("rows");
                if (rowsArray == null) {
                    airportIataList.add(cityIata);
                    return new ArrayList<>(airportIataList);
                }
                for (Object row : rowsArray) {
                    AirportIataResp airportIataResp = JSON.parseObject(String.valueOf(row), AirportIataResp.class);
                    airportIataList.add(airportIataResp.getAptiata());
                }
            }
        } catch (Exception ignored) {
            airportIataList.add(cityIata);
            return new ArrayList<>(airportIataList);
        }
        return new ArrayList<>(airportIataList);
    }
}
